﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Security.Cryptography;
using System.Web;
using System.Web.Mvc;
using Kingdee.WeksErp.DAL;
using Kingdee.WeksErp.Utility;

namespace Kingdee.WeksErp.Controllers
{
    public class AccountController : Controller
    {
        private WeksErpContext db = new WeksErpContext();
        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        [ActionName("Login")]
        public ActionResult LoginSystem(string loginId, string pwd)
        {

            try
            {
                var p = CommonHelper.GetMd5(pwd);
                var user = db.UserInfos.FirstOrDefault(u => u.LoginID == loginId);
                if (user == null || user.Pwd != p)
                {
                    return Json(new { success = false, msg = "用户名或密码错误" }, JsonRequestBehavior.AllowGet);
                }
                Session["User"] = user;
                return Json(new
                {
                    success = true,
                    msg = "登录成功！"
                }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    success = false,
                    msg = ex.Message
                }, JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult Logout()
        {
            Session["User"] = null;
            return Redirect("/");
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}